#include "../h/ObjectsContainer.h"
#include<vector>
#include <string>
using namespace std;
#include "../h/QuantityObject.h"
#include <iostream.h>


ObjectsContainer::ObjectsContainer()
{
	objectsList = vector<QuantityObject*>();
	numOfObjects = 0;
	numOfTyps = 0;
}


ObjectsContainer::~ObjectsContainer()
{
	this->clear();
}

void ObjectsContainer::clear()
{
	vector<QuantityObject*>::iterator it;
	for( it=objectsList.begin() ; it < objectsList.end();)
	{
		QuantityObject* j=*it;
		//cout<<"this QuantityObject was deleted "<<j->getAmount()<<endl;
		delete (j);
		j=0;
		objectsList.erase(it);

	}
}

void ObjectsContainer::add(string name,double amount)
{
	bool found = false;
	vector<QuantityObject*>::iterator it;
	for( it=objectsList.begin() ; it < objectsList.end(); it++ )
	{
		QuantityObject* j=*it;
		if(j->getName()==name)
		{
			j->add(amount);
			found = true;
			numOfObjects+=amount;
		}
	}
	if (!found)
	{
		QuantityObject* temp = new QuantityObject(name,amount);
		objectsList.push_back(temp);
		numOfTyps++;
		numOfObjects+=amount;
	}
}


void ObjectsContainer::add(string name,int amount) // can do problem need to check
{
	bool found = false;
	vector<QuantityObject*>::iterator it;
	for( it=objectsList.begin() ; it < objectsList.end(); it++ )
	{
		QuantityObject* j=*it;
		if(j->getName()==name)
		{
			j->add(amount);
			found = true;
			numOfObjects+=amount;
		}
	}
	if (!found)
	{
		QuantityObject* temp = new QuantityObject(name,amount);
		objectsList.push_back(temp);
		numOfTyps++;
		numOfObjects+=amount;
	}
}

void ObjectsContainer::add(string name)
{
	bool found = false;
	vector<QuantityObject*>::iterator it;
	for( it=objectsList.begin() ; it < objectsList.end(); it++ )
	{
		QuantityObject* j=*it;
		if(j->getName()==name)
		{
			j->add();
			found = true;
			numOfObjects++;
		}
	}
	if (!found)
	{
		QuantityObject* temp = new QuantityObject(name);
		objectsList.push_back(temp);
		numOfObjects++;
		numOfTyps++;
	}
}

void ObjectsContainer::remove(string name,double amount)
{
	if(amount>0)
	{
		bool found = false;
		vector<QuantityObject*>::iterator it;
		if(amount>0)
		for( it=objectsList.begin() ; it < objectsList.end(); it++ )
		{
			QuantityObject* j=*it;
			if(j->getName()==name)
			{
				j->remove(amount);
				found = true;
				numOfObjects-=amount;
				if (j->getAmount()==0)
				{
					objectsList.erase(it);
					delete (j);
					j = 0;
					numOfTyps--;
				}
			}
		}
		if (!found)
		{
			cout <<"eror not found"<<endl;
		}
	}
}


void ObjectsContainer::remove(string name,int amount)
{
	if(amount>0)
	{
		bool found = false;
		vector<QuantityObject*>::iterator it;
		for( it=objectsList.begin() ; it < objectsList.end(); it++ )
		{
			QuantityObject* j=*it;
			if(j->getName()==name)
			{
				j->remove(amount);
				found = true;
				numOfObjects-=amount;
				if (j->getAmount()==0)
				{
					objectsList.erase(it);
					delete (j);
					j = 0;
					numOfTyps--;
				}
			}
		}
		if (!found)
		{
			cout <<"eror not found"<<endl;
		}
	}
}

void ObjectsContainer::remove(string name)
{
	bool found = false;
	vector<QuantityObject*>::iterator it;
	for( it=objectsList.begin() ; it < objectsList.end(); it++ )
	{
		QuantityObject* j=*it;
		if(j->getName()==name)
		{
			j->remove();
			found = true;
			numOfObjects--;
			if (j->getAmount()==0)
			{
				objectsList.erase(it);
				delete (j);
				j = 0;
				numOfTyps--;
			}
		}
	}
	if (!found)
	{
		cout <<"eror not found"<<endl;
	}
}

double ObjectsContainer::getNameAmount(string name)
{
	if(objectsList.size()>0)//mooozzzaaarrr
	{
		bool found = false;
		vector<QuantityObject*>::iterator it;
		for( it=objectsList.begin() ; it < objectsList.end(); it++ )
		{
			QuantityObject* j=*it;
			if(j->getName()==name)
			{
				found = true;
				return (j->getAmount());
			}
		}
		if (!found)
		{
	//		cout<<"name not found"<<endl;
			return 0;
		}
	}
	return 0;
}


